
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();

var uid = 0;
var token = '';

var confirm = 1;

var score = 5;

var orderid = '';
var valuename = '已收到货品，并且验证已验收合格';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    confirms: [
      {name: '1', value: valuename, checked: 'true'}
    ],
    scores: [
      {name: '1', value: '1分'},
      {name: '2', value: '2分'},
      {name: '3', value: '3分'},
      {name: '4', value: '4分'},
      {name: '5', value: '5分', checked: 'true'}
    ],
    uid:0,  //当前登录用户Id
    token:'', //post请求时，当前登录用户token
    orderid : '',
    frm:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var titlename = tc == 1 ? '確認收貨' : '确认收货';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    var that = this;
    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function(res){
      //更新数据
      that.setData({
        uid:res.uid,
        token: res.token
      })
      uid = res.uid;
      token = res.token;
    }, inviteuid); 

    var tc = app.get_tc();
    if(tc == 1){
      valuename = '已收到貨品，並且驗證已驗收合格';
    }
    this.setData({    
      orderid: options.orderid,
      frm : options.frm,
      tc: tc
    })   
    
  },

  subForm : function(e){
      app.saveFormId(e.detail.formId);
      var that = this;    
      var comment = e.detail.value.comment;
      var privatekey = utilMd5.hexMD5(uid+'recgoods'+token);
      
      var params = {
        publickey: util.publiccode,
        ac: "recgoods",
        uid: uid,
        orderid: that.data.orderid,
        comment: comment,
        score: score,
        privatekey: privatekey
      };
      console.log('评价 参数：', params);
    util.api({
        data:params,
        method:'POST',
        success:function(responseData){
            var data = responseData.data;
            console.log('评价 返回：', data);
            var dialogToastname = that.data.tc == 1 ? '評價已提交' : '评价已提交';
            if(data.res) {
              app.dialogToast(dialogToastname, 'success', function(){
                  wx.navigateBack({
                    delta: 1
                  })     
              });
            } else {
              var dialogname = that.data.tc == 1 ? '提交失敗' : '提交失败';
              app.dialog(dialogname, 'loading');
            }
        }
    }); 
  },

  scoreChange : function(e){
    score = e.detail.value;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})